package com.xingzhe.pengji.security.controller.system;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xingzhe.pengji.framework.controller.PaginationController;
import com.xingzhe.pengji.security.domain.Group;
import com.xingzhe.pengji.security.services.GroupService;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 */
@Controller
@RequestMapping("/group")
public class GroupController extends PaginationController
{

    private Group group;
    private List<Group> groupsList;
    private ModelAndView modelAndView;
    @Autowired
    private GroupService<Group> groupService;
    private String operatorManString = "Admin";
    private final String GROUP_INDEX = "redirect:/group/index.html";

    //private final static Logger logger = LoggerFactory.getLogger(GroupController.class);

    @Override
    protected ModelAndView executePaginationController(HttpServletRequest request, HttpServletResponse response,
            int pageNumber, int pageSize) throws Exception
    {

        groupsList = groupService.queryForPageGroupList(null, "id", pageSize, pageNumber, false);
        modelAndView = new ModelAndView();
        modelAndView.addObject("groupsList", groupsList);
        modelAndView.setViewName("/jsp/group/queryPortalGroup");

        return modelAndView;
    }

    @Override
    protected int getItemCount(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        return groupService.getItemCount(group);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/add.html")
    public String addGroup()
    {
        return "/jsp/group/addGroup";
    }

    @RequestMapping(value = "/save.html")
    public String saveGroup(@ModelAttribute("group")
    Group group)
    {
        group.setCreateTime(new Date());
        group.setCreator(operatorManString);
        group.setUpdateTime(new Date());
        group.setUpdateMan(operatorManString);
        groupService.saveGroup(group);
        return GROUP_INDEX;
    }

    @RequestMapping(value = "/saveOrUpdate.html")
    //, BindingResult result
    public String saveOrUpdateGroup(@ModelAttribute("group")
    Group group)
    {
        group.setUpdateTime(new Date());
        group.setUpdateMan(operatorManString);
        groupService.updateGroup(group);
        return GROUP_INDEX;
    }

    @RequestMapping(value = "/update.html")
    public ModelAndView updateGroup(@RequestParam("groupIds")
    String groupIds)
    {

        String[] stringargs = groupIds.split(",");

        group = new Group();
        group.setId(Integer.valueOf(stringargs[0]));
        group = groupService.getGroupById(group);
        modelAndView = new ModelAndView();
        modelAndView.addObject("group", group);
        modelAndView.setViewName("/jsp/group/updateGroup");

        return modelAndView;
    }

    @RequestMapping(value = "/delete.html")
    public String delGroup(@RequestParam("groupIds")
    String groupIds)
    {

        String[] argsString = groupIds.split(",");
        group = new Group();
        for ( String string : argsString )
        {
            group.setId(Integer.valueOf(string));
            group.setUpdateTime(new Date());
            groupService.delGroup(group);
        }
        return GROUP_INDEX;
    }

    @ResponseBody
    @RequestMapping(value = "/validateName.html")
    public Object validateAccount(@RequestParam("name")
    String name)
    {
        group = new Group();
        group.setName(name);
        group = groupService.getGroupByName(group);
        if ( null == group )
        {
            return true;
        }
        return false;
    }
}
